package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.constant.AssessConstant;
import com.csun.cmny.provider.model.constant.BedStatusConstant;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.customerArchives.*;
import com.csun.cmny.provider.model.dto.familyMember.FamilyMemberSaveDto;
import com.csun.cmny.provider.model.dto.familyMember.FamilyMemberUpdateDto;
import com.csun.cmny.provider.model.dto.livingHabit.oldLivingHabitSaveDto;
import com.csun.cmny.provider.model.dto.livingHabit.oldLivingHabitUpdateDto;
import com.csun.cmny.provider.model.vo.LivingHabit.AssessmentOfLivingConditions;
import com.csun.cmny.provider.model.vo.LivingHabit.LivingHabitVo;
import com.csun.cmny.provider.model.vo.customerArchives.*;
import com.csun.cmny.provider.pojo.Bed;
import com.csun.cmny.provider.pojo.Oldman;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/customerArchives", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcCustomerArchivesController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OldmanService oldmanService;
    @Resource
    private OmcOldmanMemberArchivesService omcOldmanMemberArchivesService;
    @Resource
    private OmcFamilyMemberService familyMemberService;
    @Resource
    private OmcContractService contractService;
    @Resource
    private OmcMedicalHistoryService medicalHistoryService;
    @Resource
    private OmcDrugHistoryService drugHistoryService;
    @Resource
    private OmcFamilyMedicalHistoryService familyMedicalHistoryService;
    @Resource
    private OmcOldLivingHabitService livingHabitService;
    @Resource
    private BedService bedService;






    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增客户档案")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addOmcContract(@RequestBody @Valid OldmanArchivesDto oldmanArchivesDto, BindingResult br){
        Long userId = getLoginAuthDto().getUserId();
        Long groupId = getLoginAuthDto().getGroupId();
        String userName = getLoginAuthDto().getUserName();

        /*校验合同号是否被使用过*/
        OmcContract contract = new OmcContract();
        contract.setContractNo(oldmanArchivesDto.getContractNo());
        List<OmcContract> select = contractService.select(contract);
        if(PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("contract.no.error", null));
        }
        /*校验老人身份证号是否被录入过*/
        Oldman old = new Oldman();
        old.setOldIdCard(oldmanArchivesDto.getOldIdCard());
        Oldman selectByOld = oldmanService.selectByOld(old);
        if(PublicUtil.isNotEmpty(selectByOld)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("checkInTime.idcard.error", null));
        }

        /*新增老人信息*/
        Oldman oldman = new Oldman();
        BeanUtils.copyProperties(oldmanArchivesDto,oldman);
        oldman.setHomeId(groupId.intValue());
        oldman.setUserName(userName);
        oldman.setCreateId(userId.intValue());
        oldman.setStatus(1);
        int selective = oldmanService.insertSelective(oldman);



        //新增老人档案
        OmcOldmanMemberArchives omcOldmanMemberArchives = new OmcOldmanMemberArchives();
        omcOldmanMemberArchives.setOldmanId(oldman.getOldmanId().longValue());
        omcOldmanMemberArchives.setGroupId(groupId);
        omcOldmanMemberArchives.setCreatorId(userId);
        int save = omcOldmanMemberArchivesService.save(omcOldmanMemberArchives);


        //新增合同信息
        OmcContract omcContract = new OmcContract();
        BeanUtils.copyProperties(oldmanArchivesDto,omcContract);
        omcContract.setOldManId(oldman.getOldmanId().longValue());
        omcContract.setGroupId(groupId);
        omcContract.setUserId(userId);
        omcContract.setOperator(userName);
        contractService.save(omcContract);



        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),omcContract);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改客户档案")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper update(@RequestBody @Valid OldmanArchivesUpdateDto archivesUpdateDto, BindingResult br){




        if(PublicUtil.isNotEmpty(archivesUpdateDto.getContractNo())){
            /*校验合同号*/
            OmcContract contract = new OmcContract();
            contract.setContractNo(archivesUpdateDto.getContractNo());
            List<OmcContract> select = contractService.select(contract);


            if(PublicUtil.isNotEmpty(select)&&!select.get(0).getId().equals(archivesUpdateDto.getContractId())){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("contract.no.error", null));
            }
        }


        /*修改老人信息*/
        Oldman oldman = new Oldman();
        BeanUtils.copyProperties(archivesUpdateDto,oldman);
        oldman.setOldmanId(archivesUpdateDto.getOldmanId().intValue());
        int update = oldmanService.updateByOldId(oldman);

        /*修改合同信息*/
        OmcContract omcContract = new OmcContract();
        BeanUtils.copyProperties(archivesUpdateDto,omcContract);
        omcContract.setId(archivesUpdateDto.getContractId());
        contractService.update(omcContract);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增家属资料")
    @RequestMapping(value = "/addFamily", method = RequestMethod.POST)
    public Wrapper addFamily(@RequestBody @Valid List<FamilyMemberSaveDto> familyMemberSaveDtos, BindingResult br){


        int result=0;
        for (FamilyMemberSaveDto familyMemberSaveDto : familyMemberSaveDtos) {
            //新增家属资料
            OmcFamilyMember omcFamilyMember = new OmcFamilyMember();
            BeanUtils.copyProperties(familyMemberSaveDto,omcFamilyMember);
            omcFamilyMember.setGroupId(getLoginAuthDto().getGroupId());
            omcFamilyMember.setCreator(getLoginAuthDto().getUserName());
            omcFamilyMember.setCreatorId(getLoginAuthDto().getUserId());
            int save = familyMemberService.save(omcFamilyMember);

            //完善老人档案
            OmcOldmanMemberArchives omcOldmanMemberArchives = new OmcOldmanMemberArchives();
            omcOldmanMemberArchives.setOldmanId(familyMemberSaveDto.getOldmanId());
            omcOldmanMemberArchives.setFamilyId(omcFamilyMember.getId());
            omcOldmanMemberArchivesService.update(omcOldmanMemberArchives);

            result+=save;
        }




        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改家属资料")
    @RequestMapping(value = "/updateFamily", method = RequestMethod.POST)
    public Wrapper updateFamily(@RequestBody @Valid List<FamilyMemberUpdateDto> familyMemberUpdateDtos, BindingResult br){


        int result=0;
        for (FamilyMemberUpdateDto familyMemberUpdateDto : familyMemberUpdateDtos) {
            //修改家属资料
            OmcFamilyMember omcFamilyMember = new OmcFamilyMember();
            BeanUtils.copyProperties(familyMemberUpdateDto,omcFamilyMember);
            System.out.println("家属资料"+omcFamilyMember);
            int save = familyMemberService.update(omcFamilyMember);

            //修改老人档案
            OmcOldmanMemberArchives omcOldmanMemberArchives = new OmcOldmanMemberArchives();
            omcOldmanMemberArchives.setOldmanId(omcFamilyMember.getOldmanId());
            omcOldmanMemberArchives.setFamilyId(omcFamilyMember.getId());
            System.out.println("老人档案"+omcOldmanMemberArchives);
            omcOldmanMemberArchivesService.update(omcOldmanMemberArchives);

            result+=save;
        }




        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "删除家属资料")
    @RequestMapping(value = "/deleteFamily", method = RequestMethod.POST)
    public Wrapper deleteFamily(Long id){

        int result = familyMemberService.deleteByKey(id);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "根据老人id查询档案日志信息")
    @RequestMapping(value = "/selectOld", method = RequestMethod.POST)
    public Wrapper selectOld(Long oldmanId){

        Oldman oldman = new Oldman();
        oldman.setOldmanId(oldmanId.intValue());
        oldman.setHomeId(getLoginAuthDto().getGroupId().intValue());

        Oldman old = oldmanService.selectByOld(oldman);

        OmcContract omcContract = new OmcContract();
        omcContract.setOldManId(oldmanId);
        List<OmcContract> select = contractService.select(omcContract);



        OldmanArchivesVo oldmanArchivesVo = new OldmanArchivesVo();
        BeanUtils.copyProperties(old,oldmanArchivesVo);
        oldmanArchivesVo.setStartDate(select.get(0).getStartDate());
        oldmanArchivesVo.setEndDate(select.get(0).getEndDate());
        oldmanArchivesVo.setContractNo(select.get(0).getContractNo());
        oldmanArchivesVo.setContractId(select.get(0).getId());
        oldmanArchivesVo.setPictureCode(select.get(0).getPictureCode());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),oldmanArchivesVo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "根据老人id查询家属信息")
    @RequestMapping(value = "/selectFamilyMember", method = RequestMethod.POST)
    public Wrapper selectFamilyMember(Long oldmanId){

        OmcFamilyMember omcFamilyMember = new OmcFamilyMember();
        omcFamilyMember.setOldmanId(oldmanId);
        omcFamilyMember.setGroupId(getLoginAuthDto().getGroupId());
        List<OmcFamilyMember> list = familyMemberService.select(omcFamilyMember);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),list);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "分页查询老人信息")
    @RequestMapping(value = "/selectQueryOldman", method = RequestMethod.POST)
    public Wrapper selectQueryOldman(@RequestBody OldmanArchivesQueryDto oldmanArchivesQueryDto){

        oldmanArchivesQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        List<OldmanArchivesListVo>list=oldmanService.selectQueryOldman(oldmanArchivesQueryDto);



        PageInfo<OldmanArchivesListVo> pageInfo = new PageInfo<>(list);




        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增老人现病史")
    @RequestMapping(value = "/saveMedicaHistory", method = RequestMethod.POST)
    public Wrapper saveMedicaHistory(@RequestBody @Valid List<OmcMedicalHistoryDto> medicalHistoryDtos){

        int result=0;

        for (OmcMedicalHistoryDto medicalHistoryDto : medicalHistoryDtos) {
            OmcMedicalHistory omcMedicalHistory = new OmcMedicalHistory();
            BeanUtils.copyProperties(medicalHistoryDto,omcMedicalHistory);
            omcMedicalHistory.setIsCure(1);
            omcMedicalHistory.setGroupId(getLoginAuthDto().getGroupId());
            omcMedicalHistory.setCreator(getLoginAuthDto().getUserName());
            omcMedicalHistory.setCreatorId(getLoginAuthDto().getUserId());
            int save = medicalHistoryService.save(omcMedicalHistory);
            result+=save;
        }


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询老人现病史")
    @RequestMapping(value = "/selectMedicaHistory", method = RequestMethod.POST)
    public Wrapper selectMedicaHistory(@RequestBody @Valid MedicaHistorySelectDto medicaHistorySelectDto){

        medicaHistorySelectDto.setGroupId(getLoginAuthDto().getGroupId());
        medicaHistorySelectDto.setIsCure(1);
        List<MedicaHistoryVo> list=medicalHistoryService.selectMedicaHistory(medicaHistorySelectDto);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),list);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改老人现病史")
    @RequestMapping(value = "/updateMedicaHistory", method = RequestMethod.POST)
    public Wrapper updateMedicaHistory(@RequestBody @Valid List<OmcMedicalHistoryUpdateDto> medicalHistoryUpdateDtos){

        int result=0;

        for (OmcMedicalHistoryUpdateDto medicalHistoryUpdateDto : medicalHistoryUpdateDtos) {
            OmcMedicalHistory omcMedicalHistory = new OmcMedicalHistory();
            BeanUtils.copyProperties(medicalHistoryUpdateDto,omcMedicalHistory);
            int save = medicalHistoryService.update(omcMedicalHistory);
            result+=save;
        }


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增老人用药史")
    @RequestMapping(value = "/saveDrugHistory", method = RequestMethod.POST)
    public Wrapper saveDrugHistory(@RequestBody @Valid List<DrugHistoryDto> drugHistoryDtos){

        int result=0;

        for (DrugHistoryDto drugHistoryDto : drugHistoryDtos) {
            OmcDrugHistory omcDrugHistory = new OmcDrugHistory();
            BeanUtils.copyProperties(drugHistoryDto,omcDrugHistory);

            omcDrugHistory.setGroupId(getLoginAuthDto().getGroupId());
            omcDrugHistory.setCreator(getLoginAuthDto().getUserName());
            omcDrugHistory.setCreatorId(getLoginAuthDto().getUserId());
            int save = drugHistoryService.save(omcDrugHistory);
            result+=save;
        }


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询老人用药史")
    @RequestMapping(value = "/selectDrugHistory", method = RequestMethod.POST)
    public Wrapper selectDrugHistory(Long oldmanId){

        List<DrugHistoryVo> list=drugHistoryService.selectDrugHistory(oldmanId);




        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),list);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改老人用药史")
    @RequestMapping(value = "/updateDrugHistory", method = RequestMethod.POST)
    public Wrapper updateDrugHistory(@RequestBody @Valid List<DrugHistoryUpdateDto> drugHistoryUpdateDtos){

        int result=0;

        for (DrugHistoryUpdateDto drugHistoryUpdateDto : drugHistoryUpdateDtos) {
            OmcDrugHistory omcDrugHistory = new OmcDrugHistory();
            BeanUtils.copyProperties(drugHistoryUpdateDto,omcDrugHistory);

            int update = drugHistoryService.update(omcDrugHistory);
            result+=update;
        }


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "删除老人用药史")
    @RequestMapping(value = "/deleteDrugHistory", method = RequestMethod.POST)
    public Wrapper deleteDrugHistory(Long id){

        int  delete=drugHistoryService.deleteByKey(id);




        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),delete);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增老人过往病史")
    @RequestMapping(value = "/saveMedicaHistoryRecord", method = RequestMethod.POST)
    public Wrapper saveMedicaHistoryRecord(@RequestBody @Valid List<OmcMedicalHistoryDto> medicalHistoryDtos){

        int result=0;

        for (OmcMedicalHistoryDto medicalHistoryDto : medicalHistoryDtos) {
            OmcMedicalHistory omcMedicalHistory = new OmcMedicalHistory();
            BeanUtils.copyProperties(medicalHistoryDto,omcMedicalHistory);
            omcMedicalHistory.setIsCure(0);
            omcMedicalHistory.setGroupId(getLoginAuthDto().getGroupId());
            omcMedicalHistory.setCreator(getLoginAuthDto().getUserName());
            omcMedicalHistory.setCreatorId(getLoginAuthDto().getUserId());
            int save = medicalHistoryService.save(omcMedicalHistory);
            result+=save;
        }


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询老人过往病史")
    @RequestMapping(value = "/selectMedicaHistoryRecord", method = RequestMethod.POST)
    public Wrapper selectMedicaHistoryRecord(@RequestBody @Valid MedicaHistorySelectDto medicaHistorySelectDto){

        medicaHistorySelectDto.setGroupId(getLoginAuthDto().getGroupId());
        medicaHistorySelectDto.setIsCure(0);
        List<MedicaHistoryVo> list=medicalHistoryService.selectMedicaHistory(medicaHistorySelectDto);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),list);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改老人过往病史")
    @RequestMapping(value = "/updateMedicaHistoryRecord", method = RequestMethod.POST)
    public Wrapper updateMedicaHistoryRecord(@RequestBody @Valid List<OmcMedicalHistoryUpdateDto> medicalHistoryUpdateDtos){

        int result=0;

        for (OmcMedicalHistoryUpdateDto medicalHistoryUpdateDto : medicalHistoryUpdateDtos) {
            OmcMedicalHistory omcMedicalHistory = new OmcMedicalHistory();
            BeanUtils.copyProperties(medicalHistoryUpdateDto,omcMedicalHistory);
            int save = medicalHistoryService.update(omcMedicalHistory);
            result+=save;
        }


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增家族病史")
    @RequestMapping(value = "/saveFamilyMedicalHistory", method = RequestMethod.POST)
    public Wrapper saveFamilyMedicalHistory(@RequestBody @Valid List<FamilyMedicalHistoryDto> familyMedicalHistoryDtos){
        int result=0;

        for (FamilyMedicalHistoryDto familyMedicalHistoryDto : familyMedicalHistoryDtos) {
            OmcFamilyMedicalHistory omcFamilyMedicalHistory = new OmcFamilyMedicalHistory();
            BeanUtils.copyProperties(familyMedicalHistoryDto,omcFamilyMedicalHistory);
            omcFamilyMedicalHistory.setGroupId(getLoginAuthDto().getGroupId());
            omcFamilyMedicalHistory.setCreator(getLoginAuthDto().getUserName());
            omcFamilyMedicalHistory.setCreatorId(getLoginAuthDto().getUserId());
            int save = familyMedicalHistoryService.save(omcFamilyMedicalHistory);

            result+=save;
        }





        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询家族病史")
    @RequestMapping(value = "/selectFamilyMedicalHistory", method = RequestMethod.POST)
    public Wrapper selectFamilyMedicalHistory(Long oldmanId){


        List<FamilyMedicalHistoryVo> list=familyMedicalHistoryService.selectFamilyMedicalHistory(oldmanId);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),list);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改家族病史")
    @RequestMapping(value = "/updateFamilyMedicalHistory", method = RequestMethod.POST)
    public Wrapper updateFamilyMedicalHistory(@RequestBody @Valid List<FamilyMedicalHistoryUpdateDto> familyMedicalHistoryUpdateDtos){
        int result=0;

        for (FamilyMedicalHistoryUpdateDto familyMedicalHistoryUpdateDto : familyMedicalHistoryUpdateDtos) {
            OmcFamilyMedicalHistory omcFamilyMedicalHistory = new OmcFamilyMedicalHistory();
            BeanUtils.copyProperties(familyMedicalHistoryUpdateDto,omcFamilyMedicalHistory);

            int save = familyMedicalHistoryService.update(omcFamilyMedicalHistory);

            result+=save;
        }





        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增老人生活习惯")
    @RequestMapping(value = "/saveLivingHabit", method = RequestMethod.POST)
    public Wrapper saveLivingHabit(@RequestBody @Valid oldLivingHabitSaveDto livingHabitSaveDto){



            OmcOldLivingHabit omcOldLivingHabit = new OmcOldLivingHabit();
            BeanUtils.copyProperties(livingHabitSaveDto,omcOldLivingHabit);

            int result = livingHabitService.save(omcOldLivingHabit);





        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询老人生活习惯")
    @RequestMapping(value = "/selectLivingHabit", method = RequestMethod.POST)
    public Wrapper selectLivingHabit(Long oldmanId){


        List<LivingHabitVo> list=livingHabitService.selectLivingHabit(oldmanId);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),list);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改老人生活习惯")
    @RequestMapping(value = "/updateLivingHabit", method = RequestMethod.POST)
    public Wrapper updateLivingHabit(@RequestBody @Valid oldLivingHabitUpdateDto livingHabitUpdateDto){

            OmcOldLivingHabit omcOldLivingHabit = new OmcOldLivingHabit();
            BeanUtils.copyProperties(livingHabitUpdateDto,omcOldLivingHabit);

            int result = livingHabitService.update(omcOldLivingHabit);



        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询老人生活习惯评估结果")
    @RequestMapping(value = "/assess", method = RequestMethod.POST)
    public Wrapper assess(Long livingHabitVoId){


        AssessmentOfLivingConditions assessmentOfLivingConditions = new AssessmentOfLivingConditions();

        OmcOldLivingHabit livingHabit=livingHabitService.selectByKey(livingHabitVoId);


        if(livingHabit.getRiceDay()*livingHabit.getRiceWeek()>7){
            assessmentOfLivingConditions.setEat(AssessConstant.EAT_GRAIN_MORE);
        }else if(livingHabit.getRiceDay()*livingHabit.getRiceWeek()<5){
            assessmentOfLivingConditions.setEat(AssessConstant.EAT_GRAIN_FEWER);
        }else {
            assessmentOfLivingConditions.setEat(AssessConstant.EAT_NORMAL);
        }

        if(livingHabit.getSmokeDay()>5){
            assessmentOfLivingConditions.setSmoke(AssessConstant.SMOKE_MORE);
        }


        if(livingHabit.getSportsNummber()>5){
            assessmentOfLivingConditions.setSports(AssessConstant.SPORT_NUMBER_MORE);
        }else if(livingHabit.getSportsNummber()<3){
            assessmentOfLivingConditions.setSports(AssessConstant.SPORT_NUMBER_FEWER);
        }else {
            assessmentOfLivingConditions.setSports(AssessConstant.SPORT_NUMBER_NORMAL);
        }

        if(livingHabit.getSleepTime().equals(3)){
            assessmentOfLivingConditions.setSleep(AssessConstant.SLEEP_MORE);
        }else if(livingHabit.getSleepTime().equals(0)){
            assessmentOfLivingConditions.setSleep(AssessConstant.SLEEP_FEWER);
        }else {
            assessmentOfLivingConditions.setSleep(AssessConstant.SLEEP_NORMAL);
        }

        if(livingHabit.getBeerWineNext()>3||livingHabit.getWhiteWineNext()>2||livingHabit.getOtherWineNext()>3||livingHabit.getRedWineNext()>3||livingHabit.getYellowWineNext()>3){
            assessmentOfLivingConditions.setDrink(AssessConstant.DRINK_MORE);
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),assessmentOfLivingConditions);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "办理入住")
    @RequestMapping(value = "/checkIn", method = RequestMethod.POST)
    public Wrapper checkIn(@RequestBody @Valid ArchivesCheckIn archivesCheckIn){

        /*更改合同状况*/
        OmcContract omcContract = new OmcContract();
        BeanUtils.copyProperties(archivesCheckIn,omcContract);
        omcContract.setId(archivesCheckIn.getContractId());
        int result = contractService.update(omcContract);


        OmcContract contract = contractService.selectByKey(archivesCheckIn.getContractId());
        Oldman oldman = new Oldman();
        oldman.setOldmanId(contract.getOldManId().intValue());
        oldman.setStatus(2);
        oldmanService.updateByOldId(oldman);

        /*更改床位状态*/
        Bed bed = new Bed();
        bed.setBedId(archivesCheckIn.getBedId().intValue());
        bed.setStatus(BedStatusConstant.TO_PAY_COST);
        bedService.updateById(bed);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改办理入住")
    @RequestMapping(value = "/updatecheckIn", method = RequestMethod.POST)
    public Wrapper updatecheckIn(@RequestBody @Valid ArchivesCheckIn archivesCheckIn){

        /*更改合同状况*/
        OmcContract omcContract = new OmcContract();
        BeanUtils.copyProperties(archivesCheckIn,omcContract);
        omcContract.setId(archivesCheckIn.getContractId());
        int result = contractService.update(omcContract);


        /*更改床位状态*/
        Bed bed = new Bed();
        bed.setBedId(archivesCheckIn.getBedId().intValue());
        bed.setStatus(BedStatusConstant.TRY_TO_STAY_IN);
        bedService.updateById(bed);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询办理入住信息")
    @RequestMapping(value = "/selectcheckIn", method = RequestMethod.POST)
    public Wrapper selectcheckIn(Long  contractId){

        ArchivesCheckInVo archivesCheckInVo = new ArchivesCheckInVo();
        /*更改合同状况*/
        OmcContract omcContract = contractService.selectByKey(contractId);
        BeanUtils.copyProperties(omcContract,archivesCheckInVo);





        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),archivesCheckInVo);
    }
}
